package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.speaktoitcookbook.bean.Cookbook;
import com.speaktoitcookbook.bean.Material;
import com.speaktoitcookbook.dao.IMaterialDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * 菜谱原料数据操作实现类
 * 
 * @author 杨强
 * 
 */
public class MaterialDaoImpl implements IMaterialDao {
    private Connection conn;

    public MaterialDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Material material) {
        boolean result = false;
        String sql = "insert into `tb_material`(`cbID`, `mtType`, `mtName`, `mtAmount`) values(?, ?, ?, ?)";
        Object[] params = TransformUtil.beanTOArray(material);
        result = DBUtil.insert(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(Material material) {
        boolean result = false;
        String sql = "update `tb_material` set `cbID` = ?, `mtType` = ?, `mtName` = ?, `mtAmount` = ? where `cbID` = ? and `mtName` = ?";
        Object[] params = TransformUtil.beanTOArray(material);
        result = DBUtil.update(conn, sql, params, params[0], params[2]);
        return result;
    }

    @Override
    public boolean doDelete(long cookbookID, String name) {
        boolean result = false;
        String sql = "delete from `tb_material` where `cbID` = ? and `mtName` = ?";
        result = DBUtil.update(conn, sql, cookbookID, name);
        return result;
    }

    @Override
    public List<Material> getMaterial(long cookbookID) {
        List<Material> result = null;
        String sql = "select * from `tb_material` where `cbID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, cookbookID);
        if (null != all && all.size() > 0) {
            result = new ArrayList<Material>(all.size());
            for (Object[] objects : all) {
                Material material = new Material();
                material = (Material) TransformUtil.arrayToBean(objects,
                        Material.class);
                result.add(material);
            }
        }
        return result;
    }

    @Override
    public List<Cookbook> getCookbook(String name) {
        List<Cookbook> result = null;
        String sql = "select `cbID` from `tb_material` where `mtName` like ?";
        String[] str = name.split("\\s+");
        Set<Long> all = new HashSet<Long>();
        for (String string : str) {
            List<Object[]> cookBookID = DBUtil.query(conn, sql, "%" + string
                    + "%");
            for (Object[] objects : cookBookID) {
                all.add((Long) objects[0]);
            }
        }
        sql = "select * from `tb_cookbook` where `cbID` = ?";
        if (all.size() > 0) {
            result = new ArrayList<Cookbook>(all.size());
            for (Long id : all) {
                List<Object[]> cookBook = DBUtil.query(conn, sql, id);
                for (Object[] objects : cookBook) {
                    Cookbook cook = new Cookbook();
                    cook = (Cookbook) TransformUtil.arrayToBean(objects,
                            Cookbook.class);
                    result.add(cook);
                }
            }
        }
        return result;
    }

}
